<?php
/**
 * @author    1054229380@qq.com
 * @since    2017-7-3    Version 1.0
 **/

/**
 * 页面跳转
 **/
function linkTo($httpUrl, $msg = null, $jumpTime = 0)
{
    if (headers_sent($file, $line)) {
        echo "Headers sent in $file on line $line";
        exit;
    }
    if ($msg) {
        echo $msg;
    }
    header("refresh:{$jumpTime};url={$httpUrl}");
    exit();
}

/*获取IP*/
function getIP()
{
    if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
        $cip = $_SERVER["HTTP_CLIENT_IP"];
    } elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    } elseif (!empty($_SERVER["REMOTE_ADDR"])) {
        $cip = $_SERVER["REMOTE_ADDR"];
    } else {
        $cip = "";
    }
    return $cip;
}

/**
 * 调试输入出数组，简化输入
 **/
function e($_tmpArr)
{
    print_r($_tmpArr);
}

/**
 * 取不重复随机数，返回对应数组
 * @totals 总数量
 * @currNeeds 需要产生的数量
 * 如选取10以内的5个不同的随机数，则为getNoRepeatRand(10,5)
 **/
function getNoRepeatRand($totals, $currNeeds)
{
    if ($currNeeds == "0") {
        return;
    }
    $randId = rand(0, $totals - 1);
    $currNeeds = $currNeeds - 1;
    $currIds = getNoRepeatRand($totals, $currNeeds);
    if ($currIds == "") {
        $currIds = array();
    }
    if (in_array($randId, $currIds, true)) {
        $currNeeds = $currNeeds + 1;
        return getNoRepeatRand($totals, $currNeeds);
    }
    $currIds[] = $randId;
    return $currIds;
}

/**
 * 获取登陆后返回路径
 **/
function getLoginBackUrl()
{
    $loginBackUrl = (isset($_GET['loginBackUrl'])) ? str::getSafeData($_GET['loginBackUrl']) : "";
    if ($loginBackUrl != "") {
        $httpUrl = $loginBackUrl;
    } else {
        $allUrl = _SURL . $_SERVER['REQUEST_URI'];
        if (_RURL == "") {
            $httpUrl = str_replace("/index.php?/", "", $allUrl);
        } else {
            $httpUrl = str_replace("?//", "/", $allUrl);
        }
    }
    if (str::isStrExist($httpUrl, "member")) {
        $httpUrl = str_replace(array(
            "register",
            "login",
            "retrievePassWord"
        ), '', $httpUrl);
    }
    $httpUrl = urlencode($httpUrl);
    return $httpUrl;
}

/**
 * 判断类中是否存在指定函数
 **/
function methodExist($myclass, $mymethod)
{
    $classfunc = get_class_methods($myclass);
    $classnum = count($classfunc);
    for ($i = 0; $i < $classnum; $i++) {
        if ($classfunc[$i] == $mymethod) {
            return true;
        }
    }
    return false;
}

/**
 * 对象转数组
 **/
function obj_to_array($obj)
{
    if (is_object($obj)) {
        $obj = get_object_vars($obj);
    }
    if (is_array($obj)) {
        foreach ($obj as $key => & $value) {
            if (is_array($value)) {
                $value = obj_to_array($value);
            } elseif (is_object($value)) {
                $value = get_object_vars($value);
                $value = obj_to_array($value);
            }
        }
    }
    return $obj;
}

/**
 * 查询数组值
 * @param @arr:要查询的数组
 * @param @keyName:要查询的键值
 * @param @keyValue:要查询的值
 * @return 查询数组arr中id=10，arrGetBykey($arr,"id","10")找到即返回
 **/

function arrGetBykey($arr, $keyName, $keyValue)
{
    if ($keyName != "") {
        foreach ($arr as $val) {
            $keyId = $val[$keyName];
            if ($keyId == $keyValue) {
                return $val;
            }
        }
        return "";
    } else {
        foreach (array_keys($arr) as $key) {
            if (trim($arr[$key]) == trim($keyValue)) {
                return $key;
            }
        }
    }
}

/**
 * 计算时间差
 **/
function timeDiff($begin_time, $end_time)
{
    if ($begin_time < $end_time) {
        $starttime = $begin_time;
        $endtime = $end_time;
    } else {
        $starttime = $end_time;
        $endtime = $begin_time;
    }
    $timediff = $endtime - $starttime;
    $days = intval($timediff / 86400);
    $remain = $timediff % 86400;
    $hours = intval($remain / 3600);
    $remain = $remain % 3600;
    $mins = intval($remain / 60);
    $secs = $remain % 60;
    $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
    return $res;
}

/**
 * 判断是否为ajax请求
 *
 * */
function IS_AJAX()
{
    if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest") {
        return true;
    } else {
        return false;
    };
}

/**
 * 简单的生成随机码
 * 时间戳+四位随机数
 * */
function getRCode()
{
    $code = date('YmdHis', time()) . rand(1000, 9999);
    return $code;
}

//php获取中文字符拼音首字母
function getFirstCharter($str)
{
    if (empty($str)) {
        return '';
    }
    $fchar = ord($str{0});
    if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0});
    $s1 = iconv('UTF-8', 'gb2312', $str);
    $s2 = iconv('gb2312', 'UTF-8', $s1);
    $s = $s2 == $str ? $s1 : $str;
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
    $firstW = null;
    switch ($asc) {
        case $asc >= -20319 && $asc <= -20284:
            $firstW = 'A';
            break;
        case $asc >= -20283 && $asc <= -19776:
            $firstW = 'B';
            break;
        case $asc >= -19775 && $asc <= -19219:
            $firstW = 'C';
            break;
        case $asc >= -19218 && $asc <= -18711:
            $firstW = 'D';
            break;
        case $asc >= -18710 && $asc <= -18527:
            $firstW = 'E';
            break;
        case $asc >= -18526 && $asc <= -18240:
            $firstW = 'F';
            break;
        case $asc >= -18239 && $asc <= -17923:
            $firstW = 'G';
            break;
        case $asc >= -17922 && $asc <= -17418:
            $firstW = 'H';
            break;
        case $asc >= -17417 && $asc <= -16475:
            $firstW = 'J';
            break;
        case $asc >= -16474 && $asc <= -16213:
            $firstW = 'K';
            break;
        case $asc >= -16212 && $asc <= -15641:
            $firstW = 'L';
            break;
        case $asc >= -15640 && $asc <= -15166:
            $firstW = 'M';
            break;
        case $asc >= -15165 && $asc <= -14923:
            $firstW = 'N';
            break;
        case $asc >= -14922 && $asc <= -14915:
            $firstW = 'O';
            break;
        case $asc >= -14914 && $asc <= -14631:
            $firstW = 'P';
            break;
        case $asc >= -14630 && $asc <= -14150:
            $firstW = 'Q';
            break;
        case $asc >= -14149 && $asc <= -14091:
            $firstW = 'R';
            break;
        case $asc >= -14090 && $asc <= -13319:
            $firstW = 'S';
            break;
        case $asc >= -13318 && $asc <= -12839:
            $firstW = 'T';
            break;
        case $asc >= -12838 && $asc <= -12557:
            $firstW = 'W';
            break;
        case $asc >= -12556 && $asc <= -11848:
            $firstW = 'X';
            break;
        case $asc >= -11847 && $asc <= -11056:
            $firstW = 'Y';
            break;
        case $asc >= -11055 && $asc <= -10247:
            $firstW = 'Z';
            break;
    }
    return $firstW;
}

/**
 * @des      序列化数组base64编码返回字符串
 * @param
 * @return
 */
function customSX($arr = null)
{
    if (!is_array($arr)) {
        exit('必须是数组');
    }
    $arr = serialize($arr);
    return base64_encode($arr);
}

/**
 * @des      反编码base64，并反序列化
 * @param
 * @return
 */
function customSF($str = null)
{
    if (!is_string($str)) {
        exit('必须是字符串');
    }
    $str = base64_decode($str);

    return unserialize($str);
}


/*
 * 随机码
 * @ 几位
 * @ 类型
 * @ 返回随机码 1：纯数字    2：全英文    3：英文全小写    4 全英小写 文数字混合   5 唯一码
 *
 * */
function random_code($few = 6, $type = 1)
{
    $code = false;
    switch ($type) {
        case 1:
            $startNum = pow(10, $few - 1);
            $endNum = pow(10, $few) - 1;
            $code = rand($startNum, $endNum);
            break;
        case 2:
            $temp = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'G', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
            $code = implode(array_rand($temp, $few));
            break;
        case 3:
            $temp = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
            $code = implode(array_rand($temp, $few));
            break;
        case 4:
            $temp = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
            $code = implode(array_rand($temp, $few));
            break;
        case 5:
            $code = uniqid(rand(100, 999));
            break;// 唯一码
    }
    return $code;
}

?>